#include "visualobjectproperypage.h"
#include "gameObjects/visualobject.h"

VisualObjectProperyPage::VisualObjectProperyPage(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	connect(ui.pushButtonBrowseImage, &QPushButton::clicked, 
		this, &VisualObjectProperyPage::onBrowseImage);
	connect(ui.checkBoxAnimation, &QCheckBox::stateChanged, 
		this, &VisualObjectProperyPage::updateGui);
	connect(ui.spinBoxNumOfFrames, SIGNAL(valueChanged(int)), 
		this, SLOT(updateGui()));

	connect(ui.lineEditName, &QLineEdit::textChanged, 
		this, &VisualObjectProperyPage::parameterChanged);
	connect(ui.spinBoxHorizontalPattern, SIGNAL(valueChanged(int)), 
		this, SLOT(parameterChanged()));
	connect(ui.spinBoxNumOfFrames, SIGNAL(valueChanged(int)), 
		this, SLOT(parameterChanged()));
	connect(ui.lineEditSpeed, &QLineEdit::textChanged, 
		this, &VisualObjectProperyPage::parameterChanged);
	connect(ui.checkBoxLoop, &QCheckBox::stateChanged, 
		this, &VisualObjectProperyPage::parameterChanged);

	connect(ui.lineEditImagePath, &QLineEdit::textChanged, 
		this, &VisualObjectProperyPage::imagePathTextChanged);
}

void VisualObjectProperyPage::setVisualObject( QPointer<AbstractGameObject> visualObject )
{
	m_visualObject = visualObject;

	ui.widgetAnimationPlayer->setVisualObject(m_visualObject);

	if(m_visualObject)
	{
		QPointer<VisualObject> vo = qobject_cast<VisualObject*>(m_visualObject);
		Q_ASSERT(vo);

		blockWidgetSignals(true);
		ui.lineEditName->setText(vo->name());
		ui.lineEditImagePath->setText(vo->imageData().imagePath());
		ui.checkBoxAnimation->setChecked(vo->isAnimation());
		ui.spinBoxHorizontalPattern->setValue(vo->horizontalPattern());
		ui.spinBoxNumOfFrames->setValue(vo->numFrames());
		ui.lineEditSpeed->setText(QString("%1").arg(vo->animationSpeed()));
		ui.checkBoxLoop->setChecked(vo->isLoop());
		blockWidgetSignals(false);

		// workaround because setChecked for Animation doesn't call updateGui for unknown reason 
		updateGui();
	}

	ui.lineEditName->setFocus();
	ui.lineEditName->selectAll();
}

void VisualObjectProperyPage::onBrowseImage()
{
	QPointer<VisualObject> vo = qobject_cast<VisualObject*>(m_visualObject);
	Q_ASSERT(vo);

	QString imageFileName = QFileDialog::getOpenFileName(0, "Browse image", QString(), "Image files (*.png *.jpg)");

	if(!imageFileName.isEmpty())
	{
		QImage image = QImage(imageFileName);

		if(!image.isNull())
		{
			ui.lineEditImagePath->setText(imageFileName);

			ImageData id(ui.lineEditImagePath->text(), image);
			vo->setImageData(id);

			vo->generateAnimation();
		}
	}
}

void VisualObjectProperyPage::updateGui()
{
	ui.groupBoxAnimParams->setEnabled(ui.checkBoxAnimation->isChecked());
	ui.spinBoxHorizontalPattern->setEnabled(ui.spinBoxNumOfFrames->value() > 0);
}

void VisualObjectProperyPage::parameterChanged()
{
	QPointer<VisualObject> vo = qobject_cast<VisualObject*>(m_visualObject);
	Q_ASSERT(vo);

	vo->setName(ui.lineEditName->text());

	if(ui.spinBoxHorizontalPattern->value() > ui.spinBoxNumOfFrames->value())
	{
		ui.spinBoxHorizontalPattern->blockSignals(true);
		ui.spinBoxHorizontalPattern->setValue(ui.spinBoxNumOfFrames->value());
		ui.spinBoxHorizontalPattern->blockSignals(false);
	}

	vo->setHorizontalPattern(ui.spinBoxHorizontalPattern->value());
	vo->setNumFrames(ui.spinBoxNumOfFrames->value());
	vo->setAnimationSpeed(ui.lineEditSpeed->text().toFloat());
	vo->setLoop(ui.checkBoxLoop->isChecked());

	vo->generateAnimation();
}

void VisualObjectProperyPage::blockWidgetSignals( bool block )
{
	ui.lineEditName->blockSignals(block);
	ui.lineEditImagePath->blockSignals(block);
	ui.checkBoxAnimation->blockSignals(block);
	ui.spinBoxHorizontalPattern->blockSignals(block);
	ui.spinBoxNumOfFrames->blockSignals(block);
	ui.lineEditSpeed->blockSignals(block);
	ui.checkBoxLoop->blockSignals(block);
}

void VisualObjectProperyPage::imagePathTextChanged()
{
	ui.lineEditImagePath->setToolTip(ui.lineEditImagePath->text());
}
